package com.lanou3g.k12.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.Course;
import com.lanou3g.k12.entity.Notice;
import com.lanou3g.k12.mapper.NoticeMapper;
import com.lanou3g.k12.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    //    根据id删除一个
    @Override
    public void deleteById(long id) {
        noticeMapper.deleteById(id);
    }

    //    根据id批量删除
    @Override
    public void deleteByIds(long[] ids) {
        noticeMapper.deleteByIds(ids);
    }
    //    根据id修改
    @Override
    public void updateById(Long id,Notice notice) {
        notice.setUpdateTime(new Date());
        noticeMapper.updateById(notice);
        Notice notice1 = noticeMapper.selectNoticeById(id);
        if (notice1.getSort()==null){
            notice1.setSort(1);
        }else{
            Integer sort = notice.getSort();
            noticeMapper.updateSort(sort);
            noticeMapper.updateById(notice);
        }
    }

    //    显示所有的内容
    @Override
    public List<Notice> findAll(Integer page, Integer size) {
        List<Notice> noticeList = noticeMapper.findAll();
        return noticeList;
    }

    //    搜索框模糊查找
    @Override
    public List<Notice> findByKeyword(String keyword, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Notice> noticeList = noticeMapper.findByKeyword("%" + keyword + "%");
        return noticeList;
    }
    //　　添加一条新的公告
    @Override
    @Transactional
    public int insertNotice(Notice notice,@RequestParam(defaultValue = "1") Integer sort) {
        Date date = new Date();
        notice.setCreateTime(date);
        notice.setUpdateTime(date);
        noticeMapper.updateSort(notice.getSort());
        notice.setSort(sort);
        int insert = noticeMapper.insertNotice(notice);
        return insert;
    }

    //  根据id查找
    @Override
    public Notice selectNoticeById(Long id) {
        Notice selectId = noticeMapper.selectNoticeById(id);
        return selectId;
    }

    @Override
    public void updateSort(Integer sort) {
        noticeMapper.updateSort(sort);
    }
}

